package bg.btanev.eightyfour.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import bg.btanev.eightyfour.dao.AnalyticsDAO;
import bg.btanev.eightyfour.domain.Target;
import bg.btanev.eightyfour.service.AnalyticsService;
import bg.btanev.eightyfour.shared.analytics.DayActivityStat;
import bg.btanev.eightyfour.shared.analytics.HourActivityStat;
import bg.btanev.eightyfour.shared.analytics.WeekDayActivityStat;

/**
 *
 * @author B.Tanev
 *
 */
public class AnalyticsServiceImpl implements AnalyticsService {
	private AnalyticsDAO analytics;

	public AnalyticsServiceImpl() {
	}

	public void setAnalytics(AnalyticsDAO analytics) {
		this.analytics = analytics;
	}

	@Override
	public List<DayActivityStat> findDayActivity(Target target, Date begin, Date end) {
		List<DayActivityStat> result = analytics.dayActivityForTarget(target, begin, end);
		Collections.sort(result, new Comparator<DayActivityStat>() {
			@Override
			public int compare(DayActivityStat o1, DayActivityStat o2) {
				return o1.getDay().compareTo(o2.getDay());
			}
		});
		return result;
	}

	@Override
	public List<HourActivityStat> findHourActivity(Target target, Date begin, Date end) {
		List<HourActivityStat> result = analytics.hourActivityForTarget(target, begin, end);
		Collections.sort(result, new Comparator<HourActivityStat>() {
			@Override
			public int compare(HourActivityStat o1, HourActivityStat o2) {
				return o1.getHour() - o2.getHour();
			}
		});
		return result;
	}

	@Override
	public List<WeekDayActivityStat> findWeekDayActivity(Target target, Date begin, Date end) {
		List<WeekDayActivityStat> result = analytics.weekDayActivityForTarget(target, begin, end);
		Collections.sort(result, new Comparator<WeekDayActivityStat>() {
			@Override
			public int compare(WeekDayActivityStat o1, WeekDayActivityStat o2) {
				return o1.getWeekDay() - o2.getWeekDay();
			}
		});
		return result;
	}
}
